//
//  XLCustomNavigationBar.m
//  campus
//
//  Created by xl on 2019/12/13.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLCustomNavigationBar.h"

@interface XLCustomNavigationBar ()

@property (nonatomic, strong) XLView *topView;
@property (nonatomic, strong) XLButton *backBtn;
@property (nonatomic, strong) XLLabel *titleLB;

@end

@implementation XLCustomNavigationBar

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.topView];
        [self.topView addSubview:self.backBtn];
        [self.topView addSubview:self.titleLB];

        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(STATUSBAR_HEIGHT);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(self.frame.size.height - STATUSBAR_HEIGHT);
        }];
        [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.topView).offset(15);
            make.centerY.equalTo(self.topView);
        }];
        [_backBtn sizeToFit];
        [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.topView);
            make.centerX.equalTo(self.topView);
        }];
        [_titleLB sizeToFit];
    }
    return self;
}

#pragma mark - UIButton
- (void)clickBack {
    if (self.delegate && [self.delegate respondsToSelector:@selector(goBackEvents)]) {
        [self.delegate goBackEvents];
    }
}

#pragma mark - public
- (void)setCustomNavigationBarTitle:(NSString *)title {
    self.titleLB.text = title;
}

- (void)hideBackButton:(BOOL)hide {
    self.backBtn.hidden = hide;
}

- (void)titleColor:(UIColor *)color {
    self.titleLB.textColor = color;
}

- (void)backButtonIcon:(NSString *)icon {
    [_backBtn setImage:XLIMAGE(icon) forState:UIControlStateNormal];
}

- (void)setNavigationBackgroundColor:(UIColor *)color {
    self.backgroundColor = color;
    self.topView.backgroundColor = color;
}
#pragma mark - setter/getter
- (XLView *)topView {
    if (!_topView) {
        _topView = [XLView new];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

- (XLButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [XLButton new];
        [_backBtn setImage:XLIMAGE(@"icon_back_hei") forState:UIControlStateNormal];
        [_backBtn setCurrentButtonHotSize:CGSizeMake(80, 44)];
        [_backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCRegularWithSize:18];
        _titleLB.textColor = [UIColor blackFontColor];
        _titleLB.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLB;
}
@end
